import { ajax } from '../lib/ajax.js';
import { tools } from '../lib/tools.js';

//#region 获取修改信息的部分
let txt_title = document.getElementById('txt_title');
let txt_source = document.getElementById('txt_source');
let txt_info = document.getElementById('txt_info');
let btnOk = document.getElementById('btnOk');
let txt_topIndex = document.getElementById('txt_topIndex');

console.log(location.href);
console.log(location.search);
console.log(location.host);
let bid = location.search.replace('?bid=', '');
console.log('bid===>', bid);

ajax.get(`/crud/billboard/${bid}`, {}, (data) => {
  if (!data.success) {
    alert(data.message);
    return;
  }
  txt_title.value = data.data.username;
  txt_source.value = data.data.title;
  txt_info.value = data.data.content;
  txt_topIndex.value = data.data.topIndex;
});

btnOk.addEventListener('click', () => {
  let billboard = {
    username: txt_title.value,
    title: txt_source.value,
    content: txt_info.value,
    topIndex: txt_topIndex.value,
    bid: bid,
  };

  ajax.put('/crud/billboard', billboard, (data) => {
    alert(data.message);
  });
});

//#endregion
